define(["require", "exports", "../mve/childrenBuilder", "../mve/index", "../mve/util", "./DOM"], function (require, exports, childrenBuilder_1, index_1, util_1, DOM) {
    "use strict";
    exports.__esModule = true;
    exports.cascadingStyle = exports.styleStringify = exports.clsOf = exports.idOf = exports.domText = exports.svg = exports.dom = exports.reWriteInit = exports.reWriteEvent = exports.DOMVirtualParamReverse = exports.DOMVirtualParam = void 0;
    /**
     * 在DOM里，移动其实是少用的
     * 比如窗口，使用z-index的方式，不使用移动
     * 别的表格拖拽，似乎更是根据模型在相应区域重新生成视图
     */
    var DOMVirtualParam = /** @class */ (function () {
        function DOMVirtualParam(pel) {
            this.pel = pel;
        }
        DOMVirtualParam.prototype.append = function (el, isMove) {
            DOM.appendChild(this.pel, el, isMove);
        };
        DOMVirtualParam.prototype.remove = function (el) {
            DOM.removeChild(this.pel, el);
        };
        DOMVirtualParam.prototype.insertBefore = function (el, oldEl, isMove) {
            DOM.insertChildBefore(this.pel, el, oldEl, isMove);
        };
        return DOMVirtualParam;
    }());
    exports.DOMVirtualParam = DOMVirtualParam;
    var DOMVirtualParamReverse = /** @class */ (function () {
        function DOMVirtualParamReverse(pel) {
            this.pel = pel;
        }
        DOMVirtualParamReverse.prototype.remove = function (e) {
            DOM.removeChild(this.pel, e);
        };
        DOMVirtualParamReverse.prototype.append = function (e, isMove) {
            DOM.prefixChild(this.pel, e, isMove);
        };
        DOMVirtualParamReverse.prototype.insertBefore = function (e, old, isMove) {
            DOM.insertChildAfter(this.pel, e, old, isMove);
        };
        return DOMVirtualParamReverse;
    }());
    exports.DOMVirtualParamReverse = DOMVirtualParamReverse;
    function reWriteEvent(n, eventName, fun) {
        var v = n[eventName];
        if (mb.Array.isArray(v)) {
            n[eventName] = fun(v);
        }
        else if (v) {
            n[eventName] = fun([v]);
        }
        else {
            n[eventName] = fun([]);
        }
    }
    exports.reWriteEvent = reWriteEvent;
    function reWriteInit(v, fun) {
        if (mb.Array.isArray(v.init)) {
            v.init = fun(v.init);
        }
        else if (v.init) {
            v.init = fun([v.init]);
        }
        else {
            v.init = fun([]);
        }
    }
    exports.reWriteInit = reWriteInit;
    function addEvent(el, event) {
        if (event) {
            for (var k in event) {
                var v = event[k];
                if (mb.Array.isArray(v)) {
                    for (var _i = 0, v_1 = v; _i < v_1.length; _i++) {
                        var vv = v_1[_i];
                        DOM.event(el, k, vv);
                    }
                }
                else {
                    DOM.event(el, k, v);
                }
            }
        }
    }
    function arrayFromInit(configInit) {
        return configInit ? (Array.isArray(configInit) ? configInit : [configInit]) : [];
    }
    function buildParam(me, el, config) {
        var attr = config.attr || {};
        addEvent(el, config.event);
        if (config.cls) {
            if (attr['class']) {
                console.warn('仅使用外层cls而不使用内层class');
                delete attr['class'];
            }
            index_1.parseUtil.bind(me, config.cls, function (v) {
                var cls = mb.Array.isArray(v) ? v.join(' ') : v;
                DOM.attr(el, "class", cls);
            }, util_1.notEqualChange);
        }
        if (config.style) {
            if (attr.style) {
                console.warn("仅使用顶层的style而不使用attr.style", config);
                delete attr.style;
            }
            if (typeof (config.style) == 'function') {
                index_1.parseUtil.bind(me, config.style, function (v) {
                    DOM.attr(el, 'style', styleStringify(v));
                }, util_1.notEqualChange);
            }
            else {
                index_1.parseUtil.bindKV(me, config.style, function (k, v) {
                    DOM.style(el, k, v);
                }, util_1.notEqualChange);
            }
        }
        if (config.id) {
            if (attr.id) {
                console.warn('仅使用外层id而不使用内层attr.id');
            }
            attr.id = config.id;
        }
        /**attr属性初始化*/
        index_1.parseUtil.bindKV(me, attr, function (k, v) {
            DOM.attr(el, k, v);
        }, util_1.notEqualChange);
        var inits = config.init ? (mb.Array.isArray(config.init) ? config.init : [config.init]) : [];
        var prop = config.prop || {};
        var children = config.children ? (mb.Array.isArray(config.children) ? config.children : [config.children]) : [];
        var hasChildren = children.length > 0;
        if (hasChildren) {
            if (config.text) {
                console.warn('有children属性不能有text', config);
                delete config.text;
            }
        }
        else {
            if (config.text) {
                index_1.parseUtil.bind(me, config.text, function (v) {
                    DOM.content(el, v);
                }, util_1.notEqualChange);
            }
        }
        /***prop属性初始化 */
        if (prop.scrollTop) {
            var scrollTop_1 = prop.scrollTop;
            delete prop.scrollTop;
            inits.unshift(function () {
                index_1.parseUtil.bind(me, scrollTop_1, function (v) {
                    DOM.prop(el, 'scrollTop', v);
                }, util_1.notEqualChange);
            });
        }
        if (prop.scrollLeft) {
            var scrollLeft_1 = prop.scrollLeft;
            delete prop.scrollLeft;
            inits.unshift(function () {
                index_1.parseUtil.bind(me, scrollLeft_1, function (v) {
                    DOM.prop(el, 'scrollLeft', v);
                }, util_1.notEqualChange);
            });
        }
        index_1.parseUtil.bindKV(me, prop, function (k, v) {
            DOM.prop(el, k, v);
        }, util_1.alawaysChange);
        if (hasChildren) {
            var virtualParam = config.childrenReverse ? new DOMVirtualParamReverse(el) : new DOMVirtualParam(el);
            var childrenInit = (0, childrenBuilder_1.childrenBuilder)(me, virtualParam, children);
            if (childrenInit) {
                //子层先初始化
                inits.unshift(childrenInit);
            }
        }
        /**
         * value必须在Attr后面才行，不然type=range等会无效
         * select的value必须放在children后，不然会无效
         */
        if (config.value) {
            index_1.parseUtil.bind(me, config.value, function (v) {
                DOM.value(el, v);
            }, util_1.alawaysChange);
        }
        return {
            element: el,
            init: getInit(inits, el, me)
        };
    }
    function getInit(inits, el, me) {
        if (inits.length > 0) {
            var init_1 = function (v) {
                var destroy = v(el, me);
                if (destroy) {
                    if (typeof (destroy) == 'function') {
                        return destroy;
                    }
                    else {
                        console.warn("\u8FD4\u56DEdesroy\u4E0D\u4E3A\u51FD\u6570\uFF0C\u4E0D\u89C6\u4E3A\u751F\u547D\u5468\u671F\u51FD\u6570", destroy, v);
                    }
                }
            };
            return function () {
                return (0, util_1.arrayInit)(inits, init_1);
            };
        }
    }
    exports.dom = (0, index_1.buildElement)(function (me, p) {
        var element = DOM.createElement(p.type);
        return buildParam(me, element, p);
    });
    exports.svg = (0, index_1.buildElement)(function (me, p) {
        var element = DOM.createElementNS(p.type, "http://www.w3.org/2000/svg");
        return buildParam(me, element, p);
    });
    exports.domText = (0, index_1.buildElement)(function (me, p) {
        var element = DOM.createTextNode("");
        index_1.parseUtil.bind(me, p.text, function (v) {
            element.textContent = v;
        }, util_1.notEqualChange);
        var inits = arrayFromInit(p.init);
        addEvent(element, p.event);
        return {
            init: getInit(inits, element, me),
            element: element
        };
    });
    var idCount = 0;
    /**生成唯一ID*/
    function idOf(name) {
        return name + (idCount++);
    }
    exports.idOf = idOf;
    var clsCount = 0;
    /**生成唯一class */
    function clsOf(name) {
        return name + (clsCount++);
    }
    exports.clsOf = clsOf;
    function styleStringify(base) {
        var os = [];
        mb.Object.forEach(base, function (v, k) {
            os.push(k + ":" + v + ";");
        });
        return os.join('');
    }
    exports.styleStringify = styleStringify;
    /**
     * 像类一样在attr.style上组合
     * @param vs
     * @returns
     */
    function cascadingStyle() {
        var vs = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            vs[_i] = arguments[_i];
        }
        return function () {
            var base = {};
            for (var i = 0; i < vs.length; i++) {
                base = vs[i](base);
            }
            return base;
        };
    }
    exports.cascadingStyle = cascadingStyle;
});
